package com.wandian.assets
{
	import flash.display.*;
	import flash.media.*;
	import flash.system.*;

	public class SubLibrary extends Object
	{
		private var fLibraryLevel_str:String;
		private var fLibraryId_str:String;
		private var fLibrary_ad:ApplicationDomain;
		public static const LIBRARY_LEVEL_SOUNDS:String="soundsLibraryLevel";
		public static const LIBRARY_ID_ANY:String="anyLibraryId";
		public static const LIBRARY_LEVEL_ANY:String="anyLibraryLevel";
		public static const LIBRARY_LEVEL_BASE:String="baseLibraryLevel";
		public static const LIBRARY_LEVEL_TRANSLATION:String="translationLibraryLevel";
		public static const LIBRARY_ID_UNDEFINED:String="undifinedLibraryId";
		public static const LIBRARY_LEVEL_VIDEO:String="videoLibraryLevel";

		public function SubLibrary(param1:ApplicationDomain, param2:String="baseLibraryLevel", param3:String="undifinedLibraryId")
		{
			this.checkLibraryLevel(param2);
			this.fLibrary_ad=param1;
			this.fLibraryLevel_str=param2;
			this.fLibraryId_str=param3;
			return;
		} // end function

		public function newMovieClip(param1:String, param2:Boolean=false, param3:String="anyLibraryLevel", param4:String="anyLibraryId"):MovieClip
		{
			return MovieClip(this.newObject(param1, param2, param3, param4));
		} // end function

		public function hasLibraryLevel(param1:String):Boolean
		{
			if (param1 == LIBRARY_LEVEL_ANY || this.libraryLevel == param1)
			{
				return true;
			}
			return false;
		} // end function

		public function hasProperties(param1:String, param2:String):Boolean
		{
			return this.hasLibraryId(param2) && this.hasLibraryLevel(param1);
		} // end function

		public function get libraryLevel():String
		{
			return this.fLibraryLevel_str;
		} // end function

		protected function checkLibraryLevel(param1:String):void
		{
			if (param1 != LIBRARY_LEVEL_BASE && param1 != LIBRARY_LEVEL_VIDEO && param1 != LIBRARY_LEVEL_TRANSLATION && param1 != LIBRARY_LEVEL_SOUNDS)
			{
				throw Error("Unsupported library type: " + param1);
			}
			return;
		} // end function

		public function get applicationDomain():ApplicationDomain
		{
			return this.fLibrary_ad;
		} // end function

		public function newSound(param1:String, param2:Boolean=false, param3:String="anyLibraryLevel", param4:String="anyLibraryId"):Sound
		{
			return Sound(this.newObject(param1, param2, param3, param4));
		} // end function

		public function hasLibraryId(param1:String):Boolean
		{
			if (param1 == LIBRARY_ID_ANY || this.libraryId == param1)
			{
				return true;
			}
			return false;
		} // end function

		public function get libraryId():String
		{
			return this.fLibraryId_str;
		} // end function

		public function newObject(param1:String, param2:Boolean=false, param3:String="anyLibraryLevel", param4:String="anyLibraryId"):Object
		{
			var aObjectName_str:*=param1;
			var aAllowErrorGeneration_bl:*=param2;
			var aLibLevel_str:*=param3;
			var aLibId_str:*=param4;
			if (!this.hasLibraryLevel(aLibLevel_str))
			{
				if (aAllowErrorGeneration_bl)
				{
					throw Error("Cannot create object: requested library level is different!");
				}
				return null;
			}
			if (!this.hasLibraryId(aLibId_str))
			{
				if (aAllowErrorGeneration_bl)
				{
					throw Error("Cannot create object: requested library id is different!");
				}
				return null;
			}
			try
			{
				return new this.applicationDomain.getDefinition(aObjectName_str);
			}
			catch (aError_err:Error)
			{
			}
			if (aAllowErrorGeneration_bl)
			{
				throw Error("Object is not exists in the sublibrary: " + aObjectName_str);
			}
			return null;
		} // end function

	}
}
